Ext.ns("Ext.util.alisoft.PortalStateMgr");
Ext.util.alisoft.PortalStateMgr = function(){
	var cp = new Ext.state.CookieProvider({
		path : "/",
		expires : new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 30))
	});
	Ext.state.Manager.setProvider(cp);
	var menuStateId = "menu-state";
	var portalStateId = "portal-state";
	var myRssMenuId="myRssMenu";
	var visitedFlag="visitedFlag";
	var allPortlets=[{}];
	return {
		isFirst : function(){
			if(Ext.state.Manager.get(visitedFlag)){
				return false;
			}else{
				return true;
			}
		},
		getMenuState : function(menu){
			var state=[];
			for(var i=0;i<menu.items.getCount();i++){
				state.push([]);
				var nodes = menu.getComponent(i).getRootNode().childNodes;
				for(var j=0;j<nodes.length;j++){	
					state[i].push({id:nodes[j].id,checked:(nodes[j].checked?nodes[j].checked:false)});
				}
			}
			return state;
		},
		getPortalState : function(portal){
			var state=[];
			for(var i=0;i<portal.items.getCount();i++){
				state.push([]);
				for(var j=0;j<portal.items.itemAt(i).items.getCount();j++){
					state[i].push(portal.getComponent(i).getComponent(j).id);
				}
			}
			return state;
		},
		saveState : function(menu,portal){
			Ext.state.Manager.set(menuStateId,this.getMenuState(menu));
			Ext.state.Manager.set(portalStateId,this.getPortalState(portal));
			var nodes = menu.getComponent(1).getRootNode().childNodes;
			var myRssMenu=[];
			for (var j = 0; j < nodes.length; j++) {
				myRssMenu.push({
					id : nodes[j].id,
					text : nodes[j].text,
					url : nodes[j].url,
					portletId:nodes[j].portletId
				});
			}
			Ext.state.Manager.set(myRssMenuId,myRssMenu);	
			Ext.state.Manager.set(visitedFlag,true);
		},
		restoreMenuState : function(menu,allMenus,menuState){
			if(!menuState){
				menuState=Ext.state.Manager.get( menuStateId);
			}
			allMenus[1]=Ext.state.Manager.get(myRssMenuId);
			for(var i=0;i<allMenus.length;i++){
				if(allMenus[i]&&allMenus[i].length){
					for(var j=0;j<allMenus[i].length;j++){
						if(allMenus[i][j]){
							var checked=false;
							if(menuState[i]&&menuState[i][j]){
								checked=menuState[i][j].checked;
							}
							menu.getComponent(i).addItem({
								text : allMenus[i][j].text,
								url : allMenus[i][j].url,
								portletId : "portlet-"+i+"-"+j,
								columnIndex : i==3?2:i,
								checkchange : checkMenu,
								checked:checked
							});
						}
					}
				}
			}
		},
		restorePortalState : function(portal,allMenus,allPortlets,portalState){
			if(!portalState){
				portalState=Ext.state.Manager.get(portalStateId);
			}
			allMenus[1]=Ext.state.Manager.get(myRssMenuId);
			for(var i=0;i<2;i++){
				if(allMenus[i]&&allMenus[i].length){
					for(var j=0;j<allMenus[i].length;j++){
						if(allMenus[i][j]){
							var rssGrid = new Ext.ux.alisoft.RssGrid({
								url:allMenus[i][j].url,
								ctxDetail:ctxDetail
							});
							var portlet = new Ext.ux.alisoft.Portlet({
								id:"portlet-"+i+"-"+j,
								title : allMenus[i][j].text,
								tools : tools,
								layout : 'fit',
								items : rssGrid
							});
							allPortlets[portlet.id] = portlet;
						}
					}
				}
			}
			for(var i=0;i<portalState.length;i++){
				for(var j=0;j<portalState[i].length;j++){
					var portlet = allPortlets[portalState[i][j]];
					if(portlet){
						portal.insertPortlet(portlet,i);
						portlet.show();
					}
				}
			}
			return allPortlets;
		},
		restoreState : function(menu,portal,allMenus,allPortlets,menuState,portalState){
			this.restoreMenuState(menu,allMenus,menuState);
			return this.restorePortalState(portal,allMenus,allPortlets,portalState);
		}
	}
}();